package com.suanfa.collect;

import com.alibaba.fastjson.JSON;

import java.util.ArrayList;
import java.util.List;

/**
 * 数组全排列
 */
public class Permute {

    public static void main(String[] args) {
        int[] arr = {1,2,3};
        List<Integer> list = new ArrayList<>();
        List<List<Integer>> ans = new ArrayList<>();
        int[] used = new int[arr.length];
        new Permute().permute(arr, list, ans, used);
        System.out.println(JSON.toJSONString(ans));

    }



    public void permute(int[] arr, List<Integer> list, List<List<Integer>> ans, int[] used){
        if(list.size() == arr.length){
            ans.add(new ArrayList<>(list));
            return;
        }

        for(int i=0; i< arr.length; i++){
            if(used[i]==0){
                list.add(arr[i]);
                used[i] = 1;
                permute(arr, list, ans, used);
                list.remove(list.size()-1);
                used[i] = 0;
            }
        }


    }
}
